package com.driving.code.controller;

import com.driving.code.dto.CollectionDto;
import com.driving.code.dto.TypeDto;
import com.driving.code.entity.Collection;
import com.driving.code.entity.ResponseModel;
import com.driving.code.entity.TUser;
import com.driving.code.service.CollectionService;
import com.driving.code.vo.CollectionVo;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Resource
    private CollectionService collectionService;

    /**
     * 根据用户ID查询出用户的收藏科目集合
     * @param session
     * @return
     */
    @RequestMapping("/type")
    public ResponseModel<List<TypeDto>> selectTypeName(HttpSession session){
        TUser user = (TUser)session.getAttribute("user");
        List<TypeDto> typeNameList = collectionService.selectTypeByUserId(user.getUserId());
        if (!ObjectUtils.isEmpty(typeNameList)){
            return ResponseModel.success(typeNameList);
        }else {
            return ResponseModel.error("没有数据");
        }
    }

    /**
     * 根据id获取用户收藏的所有题目
     * @param session
     * @return
     */
    @RequestMapping("/question")
    public ResponseModel<List<CollectionDto>> selectQuestionByUserId(HttpSession session){
        TUser user = (TUser) session.getAttribute("user");
        List<CollectionDto> collectionDtoList = collectionService.selectQuestionByUserId(user.getUserId());
        if (!ObjectUtils.isEmpty(collectionDtoList)){
            return ResponseModel.success(collectionDtoList);
        }else {
            return ResponseModel.error("没有数据");
        }
    }
    @RequestMapping("/question/{typeId}")
    public ResponseModel<List<CollectionDto>> selectQuestionByUserIdAndType(HttpSession session,@PathVariable("typeId") Integer typeId){
        TUser user = (TUser) session.getAttribute("user");
        List<CollectionDto> collectionDtoList = collectionService.selectQuestionByUserIdAndType(user.getUserId(),typeId);
        if (!ObjectUtils.isEmpty(collectionDtoList)){
            return ResponseModel.success(collectionDtoList);
        }else {
            return ResponseModel.error("没有数据");
        }
    }

    @RequestMapping("/save")
    public ResponseModel insertCollectionQuestion(HttpSession session, @RequestBody CollectionVo collectionVo){
        TUser user = (TUser) session.getAttribute("user");
        if (!ObjectUtils.isEmpty(user)){
            collectionVo.setUserId(user.getUserId());
            collectionService.saveCollectionQuestion(collectionVo);
            return ResponseModel.success("收藏成功");
        }else {
            return ResponseModel.error("请先登录");
        }
    }
}
